<template>
  <div>
    <PageTemplate>
      <div slot="content">
        <PageEditCollapse :is-search="false" :default-collapse="true">
          <div slot="title" class="page-edit-colllapse-title">
            <div style="width: 200px">{{ $t("cm.baseinfo") }}</div>
          </div>
          <div>
            <!-- 高级搜索具体内容 -->
            <el-form
              :model="messageTemplate"
              label-position="right"
              label-width="150px"
              class="page-edit-colllapse-form"
              :rules="rules"
              ref="messageTemplate"
            >
              <el-row>
                <el-col :span="12">
                  <el-form-item
                    :label="$t('sys.msg_template_name')"
                    prop="templateName"
                  >
                    <el-input
                      v-model="messageTemplate.templateName"
                      :disabled="false"
                    ></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="12" v-show="false">
                  <el-form-item :label="$t('sys.msg_template_type')">
                    <el-input
                      v-model="messageTemplate.templateType"
                      :disabled="false"
                    ></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="4"> </el-col>
                <el-col :span="16">
                  <el-form-item>
                    <el-button
                      type="primary"
                      round
                      v-show="lastOperator"
                      @click="onSetEdText('$(lastOperator)')"
                      >上一环节处理人</el-button
                    >
                    <el-button
                      type="success"
                      round
                      v-show="submitTime"
                      @click="onSetEdText('$(submitTime)')"
                      >提交日期</el-button
                    >
                    <el-button
                      type="danger"
                      round
                      v-show="operator"
                      @click="onSetEdText('$(operator)')"
                      >操作人</el-button
                    >
                    <el-button
                      type="warning"
                      round
                      v-show="title"
                      @click="onSetEdText('$(title)')"
                      >主题</el-button
                    >
                    <el-button
                      type="danger"
                      round
                      v-show="meetingTime"
                      @click="onSetEdText('$(meetingTime)')"
                      >会议时间</el-button
                    >
                    <el-button
                      type="success"
                      round
                      v-show="convener"
                      @click="onSetEdText('$(convener)')"
                      >召开人</el-button
                    >
                          <el-button
                      type="primary"
                      round
                      v-show="meetingAddress"
                      @click="onSetEdText('$(meetingAddress)')"
                      >会议地址</el-button
                    >
                    
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="12">
                  <el-form-item
                    :label="$t('sys.msg_template_Content')"
                    prop="templateContent"
                  >
                  <!-- <BasCkeditor :content="messageTemplate.templateContent" ref="contactUs" ></BasCkeditor> -->
                   <!-- <el-input
                      v-model="messageTemplate.templateContent"
                    ></el-input>` -->
                    <el-input
                      ref="remark"
                      type="textarea"
                      :rows="2"
                      v-model="messageTemplate.templateContent">
                    </el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="12"> </el-col>
              </el-row>
            </el-form>
            <!-- 查询和重置按钮-->
          </div>
        </PageEditCollapse>
        <el-row class="row-empty-10"></el-row>
        <el-row>
          <el-button type="primary" @click="submitForm('messageTemplate')" :loading="submiting" class="loading-submiting">
            {{ $t("cm.commit") }}
          </el-button>
          <el-button type="primary" @click="toList">
            {{ $t("cm.return") }}
          </el-button>
        </el-row>
      </div>
    </PageTemplate>
  </div>
</template>

<script>
import { ALERT } from "@/utils/responseHandle";
import * as Constant from "@/utils/constant";
import PageTemplate from "@/components/common/PageTemplate";
import PageEditCollapse from "@/components/common/PageEditCollapse";
import HasBtn from "@/mixins/HasBtn";
import { mapState } from "vuex";
import { COMMON } from "@/utils/commonUtil";
import Submiting from '@/mixins/Submiting'
import sys_manage from "@/api/sys_manage";
import BasCkeditor from '../common/BasCkeditor'
export default {
  components: {
    PageTemplate,
    PageEditCollapse,
    BasCkeditor
  },
  mixins: [Submiting],
  data() {
    return {
      lastOperator: false,
      submitTime: false,
      operator: false,
      title: false,
      meetingTime: false,
      meetingAddress:false,
      convener: false,
      messageTemplate: {
        id: null,
        templateName: null,
        templateContent: null,
        templateType: null,
      },
      rules: {
        templateName: [{ required: true, message: "必填项", trigger: "blur" }],
        templateContent: [
          { required: true, message: "必填项", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    if (this.$router.history.current.params.id !== Constant.NEW_PARAMS) {
      this.sysMsgTemplateGet(this.$router.history.current.params.id);
    }
  },
   
  methods: {
    onSetEdText(str) {
      this.messageTemplate.templateContent =
        this.messageTemplate.templateContent + str;
    },
    sysMsgTemplateGet(id) {
      sys_manage.sysMsgTemplateGet({ id }, (result) => {
        if (result.code === Constant.RT_SUCCESS) {
          this.messageTemplate = result.data;
          this.setVisButton(this.messageTemplate.templateType);
        } else {
          // _this.$t('cm.tips')
          ALERT.error(result.msg);
        }
      });
    },
    setVisButton(templateType) {
      if (templateType == "tsjy-xmsk") {
        //调试经营管理-项目收款进度通知
        this.title = true;
      } else if (templateType == "tsjy-xmjd") {
        this.title = true;
        //调试经营管理-项目更新进展通知
      } else if (templateType == "dbyj") {
        this.lastOperator = true;
        this.submitTime = true;
        this.operator = true;
        this.title = true;
      } else if (templateType == "hse-dx") {
        this.title = true;
        this.meetingTime = true;
        this.convener=true;
        this.meetingAddress=true;
      }
    },
    submitForm(formName) {
      if (COMMON.isNullOrUndefined(this.messageTemplate.id)) {
        this.save(formName);
      } else {
        this.update(formName);
      }
    },
    save(formName) {
      this.openSubmiting()
      this.$refs[formName].validate((valid, error) => {
        if (valid) {
          sys_manage.sysMsgTemplateSave(this.messageTemplate, (result) => {
            if (result.code === Constant.RT_SUCCESS) {
              ALERT.success(result.msg);
              this.toList();
            } else {
              // _this.$t('cm.tips')
              ALERT.error(result.msg);
            }
            this.closeSubmiting()
          });
        } else {
          this.closeSubmiting()
          return false;
        }
      });
    },
    update(formName) {
      this.openSubmiting()
      this.$refs[formName].validate((valid, error) => {
        if (valid) {
          sys_manage.sysMsgTemplateUpdate(this.messageTemplate, (result) => {
            if (result.code === Constant.RT_SUCCESS) {
              ALERT.success(result.msg);
              this.toList();
            } else {
              // _this.$t('cm.tips')
              ALERT.error(result.msg);
            }
            this.closeSubmiting()
          });
        } else {
          this.closeSubmiting()
          return false;
        }
      });
    },
    toList() {
      COMMON.routerTo(Constant.ROUTE_SYS_MSG_TEMPLATE.index, this.$router);
    },
  },
  mounted() {},
  computed: {},
};
</script>

<style lang="scss">
</style>
